package com.whfc.fse.queue;

import com.alibaba.fastjson.JSON;
import com.whfc.common.constant.QueueConst;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.spring.AppContextUtil;
import com.whfc.fse.dao.FseCraneMapper;
import com.whfc.fse.dao.FseCraneRecordMapper;
import com.whfc.fse.dto.FseCraneRecordDTO;
import com.whfc.fse.entity.FseCrane;
import com.whfc.fse.entity.FseCraneRecord;
import com.whfc.fse.event.SyncCraneRecordEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 处理硬件上传的 塔机吊装记录
 *
 * @author sun_guodong
 * @date 2020-05-26
 */
@Component
@RabbitListener(queuesToDeclare = {@Queue(name = QueueConst.FSE_CRANE_RECORD_DATA)}, concurrency = "1-2",containerFactory = "baseFactory")
public class FseCraneRecordDataQueueListener {

    private static final Logger logger = LoggerFactory.getLogger(FseCraneRecordDataQueueListener.class);

    @Autowired
    private FseCraneMapper fseCraneMapper;

    @Autowired
    private FseCraneRecordMapper fseCraneRecordMapper;

    @RabbitHandler
    public void handle(String msg) {
        try {
            logger.info("消息消费者处理塔机吊装记录,msg:{}", msg);
            FseCraneRecordDTO craneRecordDTO = JSON.parseObject(msg, FseCraneRecordDTO.class);
            String sn = craneRecordDTO.getSn();
            String platform = craneRecordDTO.getPlatform();
            FseCrane fseCrane = fseCraneMapper.selectByPlatformAndSn(platform, sn);
            if (fseCrane == null) {
                throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "该塔机不存在,sn:" + sn);
            }

            // 验证数据上报时间
            Date time = craneRecordDTO.getTime();
            if (time != null) {
                Date lastUpTime = fseCraneRecordMapper.getLastUpTimeByCraneId(fseCrane.getId());
                if (lastUpTime != null && lastUpTime.getTime() >= time.getTime()) {
                    logger.warn("数据上报时间小于最后上报时间, sn:{}, time:{}, lastTime:{}", sn, time, lastUpTime);
                    return;
                }
            }
            FseCraneRecord fseCraneRecord = new FseCraneRecord();
            BeanUtils.copyProperties(craneRecordDTO, fseCraneRecord);
            Integer craneId = fseCrane.getId();
            fseCraneRecord.setCraneId(craneId);
            fseCraneRecordMapper.insertSelective(fseCraneRecord);

            //推送吊装记录事件
            SyncCraneRecordEvent event = new SyncCraneRecordEvent(fseCrane.getDeptId(), craneRecordDTO);
            AppContextUtil.context().publishEvent(event);
        } catch (Exception e) {
            logger.error("消息消费者处理塔机吊装记录失败", e);
        }
    }
}
